<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改课程章节')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-course-edit" th:object="${lessonCourse}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">章节名称：</label>
                <div class="col-sm-8">
                    <input id="name" name="name" th:field="*{name}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select id="resType" name="resType" class="form-control m-b" th:with="type=${@dict.getType('lesson_course_res_category')}" >
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{resType}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">上传音视频：</label>
                <div class="col-sm-8">
                    <input id="vodId" type="hidden" name="vodId"/>
                    <input id="resSize" type="hidden" name="resSize"/>
                    <input id="file" name="file" type="file" class="file" data-show-preview="true"  data-show-caption="false"  data-show-upload="false" data-show-close="false">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">排序：</label>
                <div class="col-sm-8">
                    <input name="sort" th:field="*{sort}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">是否免费试看/听：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid"/>
                    <input type="checkbox" id="free" th:checked="${lessonCourse.free == 0 ? false : true}">
                    <span></span>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
        var prefix = ctx + "lesson/course";
        $("#form-course-edit").validate({
            focusCleanup: true
        });

        var allowedFileExtensions = ["mp3", "mp4"];
		var dropZoneTitle = "支持mp3,mp4上传";

		$("#file").fileinput({
            'uploadUrl': '/lesson/course/uploadMedia?courseName=' + $('#name').val(),
            'dropZoneTitle': dropZoneTitle,
            'allowedFileExtensions': allowedFileExtensions
        }).on("fileuploaded", function(e, data, previewiId, index){
        	 $('#vodId').val(data.response.vodId);
        	 if(!$('#name').val() && data.response.courseName)
        	 {
        	   $('#name').val(data.response.courseName);
        	 }
        	 $('#resType').val(data.response.resType);
        	 $('#resSize').val(data.files[0].size);
        }).on("filebatchselected", function(event, files) {
            $(this).fileinput("upload");
        });

        function submitHandler() {
            if ($.validate.form()) {
                var data = $('#form-course-edit').serializeArray()
                var free = $("input[id='free']").is(':checked') == true ? 1 : 0;
            	data.push({"name": "free", "value": free});
                $.operate.save(prefix + "/edit", data);
            }
        }

    </script>
</body>
</html>